package tp.pr2;


public class Interpreter {
	/**	
	 * Genera la instruccion a partir de una linea de texto introducida como comando.
	 * @param 	line a string with the user input
	 * @return	instruction: Resultado de parsear el comando
	 */
	public static Instruction generateInstruction (String line){
		//Pasamos a mayusculas para poder comparar con las ctes de comandos (que están en mayusculas) 
		String[] command = line.toUpperCase().split(" ");
		
		//comprobamos que la instrucción tenga una longitud válida para no dar errores de acceso a memoria inexistente
		//y después comprobamos el primer miembro con la lista de comandos posibles
		if (command.length == 0) 
			return new Instruction();

		switch (command[0]){
		case (COMMAND_MOVE):	return new Instruction(Action.MOVE);
		case (COMMAND_HELP):	return new Instruction(Action.HELP);
		case (COMMAND_QUIT):	return new Instruction(Action.QUIT);
		case (COMMAND_TURN):	{
			//de nuevo hacemos las mismas comprobaciones inciales para el segundo miembro de la entrada
			if (command.length == 1) 
				return new Instruction(Action.TURN,Rotation.UNKNOWN);
			
			switch (command[1]) { //LEFT RIGHT UNKNOWN
			case (COMMAND_TURN_LEFT):	return new Instruction(Action.TURN,Rotation.LEFT);
			case (COMMAND_TURN_RIGHT):	return new Instruction(Action.TURN,Rotation.RIGHT);
			default:					return new Instruction(Action.TURN,Rotation.UNKNOWN);
			}
		}
		case (COMMAND_PICK): return hasId(Action.PICK,line.split(" "));
		case (COMMAND_SCAN): {
			if (command.length == 1)
				return new Instruction(Action.SCAN);
			else
				return hasId(Action.SCAN,line.split(" "));
		
		}
		case (COMMAND_OPERATE): return hasId(Action.OPERATE,line.split(" "));
		default : return new Instruction();
		}
	}
	
	/**
	 * Generates an instruction for an action that has no null ID field
	 * @param action an action that needs an "ID" parameter
	 * @param command the splitted user input
	 * @return the newly created instruction with the input parameters
	 */
	private static Instruction hasId(Action action, String[] command) {
		if (command.length == 1) 
			return new Instruction(action,"");
		else
			return new Instruction(action,command[1]);
	}
	
	/**	Devuelve la ayuda
	 * 
	 * @return HELP: String con la ayuda
	 */
	public static String interpreterHelp (){
		return HELP;
	}
	

	
	//constantes de comandos para poder cambiarlas luego a otros idiomas
	private static final String COMMAND_MOVE = "MOVE";
	private static final String COMMAND_TURN = "TURN";
	private static final String COMMAND_TURN_LEFT = "LEFT";
	private static final String COMMAND_TURN_RIGHT = "RIGHT";
	private static final String COMMAND_HELP = "HELP";
	private static final String COMMAND_QUIT = "QUIT";
	private static final String COMMAND_PICK = "PICK";
	private static final String COMMAND_OPERATE = "OPERATE";
	private static final String COMMAND_SCAN = "SCAN";
	
	
	//line separator
	public static final String LINE_SEPARATOR = System.getProperty("line.separator");
	
	//constantes de la ayuda
	private static final String HELPTAB = "     ";
	private static final String HELP = "The valid instructions for "+ "WALL-E" /*RobotEngine.ROBOT_NAME*/ + " are:" +
			LINE_SEPARATOR + HELPTAB + COMMAND_MOVE + 
			LINE_SEPARATOR + HELPTAB + COMMAND_TURN+" <" + COMMAND_TURN_LEFT + " | " + COMMAND_TURN_RIGHT + ">" + 
			LINE_SEPARATOR + HELPTAB + COMMAND_PICK + " <id>" +
			LINE_SEPARATOR + HELPTAB + COMMAND_SCAN + " [ <id> ]" +
			LINE_SEPARATOR + HELPTAB + COMMAND_OPERATE + " <id>" +
			LINE_SEPARATOR + HELPTAB + COMMAND_HELP + 
			LINE_SEPARATOR + HELPTAB + COMMAND_QUIT + 
			LINE_SEPARATOR; //TODO line separator y "WALL·E" sobran, están  para el validador

}
